package com.cuikaipeng.controller;

import java.lang.ProcessBuilder.Redirect;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor.TURQUOISE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuikaipeng.entity.Plant;
import com.cuikaipeng.entity.PlantType;
import com.cuikaipeng.service.PlantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class PlantController {
	@Autowired 
	private PlantService ser;
	
	
	@RequestMapping("list")
	public String list(@RequestParam(defaultValue="1")Integer pageNum,Plant plant,Model m){
		PageHelper.startPage(pageNum, 2);
		List<Plant> list = ser.list(plant);
		PageInfo<Plant> info = new PageInfo<Plant>(list);
		m.addAttribute("list", list);
		m.addAttribute("pg", info);
		m.addAttribute("p", plant);
		
		return "list";
	}
	
	
	@RequestMapping("toadd")
	public String toadd(Model m){
		m.addAttribute("p", new Plant());
		return "add";
	}
	
	
	@RequestMapping("add")
	public String add(@ModelAttribute("p")Plant p){
		ser.add(p);
		
		return "redirect:list";
		
	}
	
	
	
	@RequestMapping("ttlist")
	@ResponseBody
	public List<PlantType> tlist(){
		 List<PlantType> tlist =ser.tlist();
		
		return tlist;
		
	}
	
	@RequestMapping("toupdate")
	public String toupdate(Model m,Integer id){
		Plant p = ser.getplant(id);
		m.addAttribute("p", new Plant());
		m.addAttribute("p", p);
		return "update";
	}
	@RequestMapping("update")
	public String update(@ModelAttribute("p")Plant p){
		System.out.println(p);
		ser.update(p);
		
		return "redirect:list";
		
	}
	
	@RequestMapping("del")
	@ResponseBody
	public boolean del(String ids){
		System.out.println(ids);
		ser.del(ids);
		try {
			ser.del(ids);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return false;
	}
}
